package com.hk.Mgr.web.controller.remoteCall;

import com.hk.Mgr.web.dto.CouponDTO;
import com.hk.base.BaseCtrl;
import com.hk.commons.entity.Parameter;
import com.hk.commons.vo.JsonResult;
import com.hk.service.order.IParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Antinomy on 17/4/7.
 */
@Controller
@RequestMapping(value = BaseCtrl.App + "/financial")
public class FinancialAPIController {
    @Value("${activity.baseUrl}")
    private String baseUrl;

    private RestTemplate template = new RestTemplate();

    @Autowired
    private IParameter parmService;

    private static final String SEND_CARD_ID = "SEND_CARD_ID";

    @RequestMapping(value = "/checkIFA/{financialId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult check(@PathVariable Long financialId) {
        String url = baseUrl + "api/financial/becomeIFA/" + financialId;

        JsonResult result = template.getForObject(url, JsonResult.class);
        return result;
    }

    @RequestMapping(value = "/cancelSendCoupon/{financialId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult cancelSend(@PathVariable Long financialId) {
        String url = baseUrl + "/api/financial/becomeIFA/" + financialId + "/giveUpSendCoupon";

        CouponDTO dto = createCouponDTO();

        JsonResult result = template.postForObject(url, dto, JsonResult.class);
        return result;
    }


    @RequestMapping(value = "/sendCoupon/{financialId}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult send(@PathVariable Long financialId) {
        String url = baseUrl + "/api/financial/becomeIFA/" + financialId + "/sendCoupon";

        CouponDTO dto = createCouponDTO();

        JsonResult result = template.postForObject(url, dto, JsonResult.class);
        return result;
    }

    private CouponDTO createCouponDTO() {
        CouponDTO dto = new CouponDTO();
        Parameter couponParm = this.parmService.getByName(SEND_CARD_ID);
        Long couponId = Long.valueOf(couponParm.getValue01());
        dto.setCardId(couponId);
        return dto;
    }
}
